#include <stdio.h>
#include "database.h"
#include "mqtt_json.h"
#include "dp_table.h"
#include "shmem.h"
#include "node.h"
dp_node_p dp;

int main(int argc, char const *argv[])
{
    // 创建共享内存
    struct shm_param shm;
    int flag = shm_init(&shm, "shm", 1024);
    if (flag)
    {
        printf("shm_init error\n");
        return -1;
    }
    dp = shm_getaddr(&shm);

    // 解析点表,并初始化共享内存
    dp_table_parse(dp);

    // 创建(打开)数据库（后续可以封装为函数）
    database_init();

    // 建立MQTT连接
    MQTT_init();
    MQTT_aiot_init();
    
    // 开启服务线程
    //存储线程
    pthread_t report, storage,aiot_report;
    int *param = malloc(sizeof(int));
    *param = type;
    pthread_create(&storage, NULL, storage_thread,NULL);
    pthread_create(&report,NULL,report_thread,param);
    pthread_create(&aiot_report,NULL,aiot_report_thread,param);
    while (1)
        ;

    MQTTClient_unsubscribe(client, TOPIC_S);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    sqlite3_close(db);
    return 0;
}
